package com.gla.giliair.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gla.giliair.dao.DiscountDao;
import com.gla.giliair.entity.Company;
import com.gla.giliair.entity.Discount;
import com.gla.giliair.entity.DiscountExample;
import com.gla.giliair.service.CompanyService;
import com.gla.giliair.service.DiscountService;
import com.gla.giliair.util.BeanUtil;
import com.gla.giliair.util.MapGenerator;
import com.gla.giliair.web.vo.DiscountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 11:11
 */
@Service
public class DiscountServiceImpl implements DiscountService {
    @Autowired
    private DiscountDao discountDao;

    @Autowired
    private CompanyService companyService;

    @Override
    public Discount selectDiscountById(Long discountId) {
        if (discountId != null) {
            DiscountExample discountExample = new DiscountExample();
            discountExample.or().andDiscountIdEqualTo(discountId).andIsDeletedEqualTo(0);
            List<Discount> discountList = discountDao.selectByExample(discountExample);
            if (discountList.isEmpty()) {
                return null;
            }
            return discountList.get(0);
        }
        return null;
    }

    @Override
    public List<Discount> selectDiscountsByCompanyId(Long companyId) {
        if (companyId != null) {
            DiscountExample discountExample = new DiscountExample();
            discountExample.setOrderByClause("discount_id desc");
            discountExample.or().andCompanyIdEqualTo(companyId).andIsDeletedEqualTo(0);
            List<Discount> discountList = discountDao.selectByExample(discountExample);
            return discountList;
        }
        return null;
    }

    @Override
    public Map<String, Object> findDiscountsByCompanyId(Long companyId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Discount> discountList = selectDiscountsByCompanyId(companyId);
        PageInfo pageInfo = new PageInfo(discountList);
        if (!discountList.isEmpty()) {
            List<DiscountVO> discountVOList = BeanUtil.copyList(discountList, DiscountVO.class);
            pageInfo.setList(discountVOList);
            return MapGenerator.getResWithData(200, "查询航空公司所属折扣方案列表成功！", pageInfo);
        } else {
            return MapGenerator.getRes(201, "查询结果为空！");
        }
    }

    @Override
    public Map<String, Object> addDiscount(Discount discount) {
        Company companyFromDB = companyService.selectCompanyById(discount.getCompanyId());
        if (companyFromDB == null) {
            return MapGenerator.getRes(500, "航空公司不存在！");
        }
        //填充创建、更新日期
        Date nowDate = new Date();
        discount.setCreateTime(nowDate);
        discount.setUpdateTime(nowDate);
        if (discountDao.insertSelective(discount) > 0) {
            return MapGenerator.getRes(200, "添加航空公司所属折扣方案成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }

    @Override
    public Map<String, Object> deleteDiscount(Long discountId) {
        Discount discountFromDB = selectDiscountById(discountId);
        if (discountFromDB == null) {
            return MapGenerator.getRes(500, "折扣方案不存在！");
        }
        discountFromDB.setIsDeleted(1);
        discountFromDB.setUpdateTime(null);
        if (discountDao.updateByPrimaryKeySelective(discountFromDB) > 0) {
            return MapGenerator.getRes(200, "删除航空公司所属折扣方案成功！");
        }
        return MapGenerator.getRes(520, "内部错误！");
    }
}
